const uuid = require("uuid/v4");
const crypto = require("crypto");

exports.uuid = () => {
    return uuid().replace(/\-/g, "");
};



exports.toTuoFeng = obj => {
    const reg = /(\-|\_)(\w)/g;
    if (Array.isArray(obj) && obj.length > 0) {
        let arr = [];
        for (let o of obj) {
            let res = {};
            for (let key in o) {
                let keyName = key.replace(reg, (...args) => args[2].toUpperCase());
                res[keyName] = o[key];
            }
            arr.push(res);
        }
        return arr;
    }
    if (Object.keys(obj).length == 0) {
        return {};
    }
    let res2 = {};
    for (let j in obj) {
        let keyName = j.replace(reg, (...args) => args[2].toUpperCase());
        res2[keyName] = obj[j]
    }
    return res2;
};

exports.sendMsg = async (vCode, phone) => {
    let url = "https://feginesms.market.alicloudapi.com/codeNotice?param=" + vCode + "&phone=" + phone + "&sign=500064&skin=900115";
    const result = await axios.get(url, {
        headers: {
            Authorization: config.code
        }
    })
    return result.data;
}


// 驼峰转换下划线
exports.todbLine = (obj) => {
    let dbStr = {}
    for (let key in obj) {
        let name = key.replace(/([A-Z])/g, "_$1").toLowerCase();
        dbStr[name] = obj[key];
    }
    return dbStr
}



exports.normalMD5 = (str) => {
    const MD5 = crypto.createHash('md5')
    MD5.update(str)
    str = MD5.digest('hex')
    return str
}

exports.numAdd = (n1, n2) => {
    let num1 = BigNumber(n1);
    let num = BigNumber(n2);
    return num1.plus(num).toNumber();
}